var TODAY;
$(function() {
    if (TODAY) {
        $("#date input").datepicker()
    } else {
        function a(b) {
            if (b.children(":eq(0)").attr("colspan")) {
                return true
            }
            return false
        }
        $("table.report tbody tr").click(function() {
            $(this).toggleClass("checked")
        });
        $("table.report").each(function() {
            var f = $(this);
            var b = f.find("tbody tr");
            var h = f.find("thead .count").index() - 1;
            var d = [];
            var e = 16;
            if (f.data("colspan")) {
                e = f.data("colspan")
            }
            if (b.length > 0) {
                b.each(function() {
                    $(this).children(":not(.info)").each(function(m) {
                        var l = $(this);
                        var n = l.children();
                        if (n.length == 1) {
                            l = n
                        }
                        var k = Number(l.text());
                        if (isNaN(k)) {
                            return
                        }
                        l.text(LIBS.money(k, 1, m != 0 && !l.hasClass("int")));
                        d[m] = (d[m] ? d[m] : 0) + k
                    })
                })
            }
            e = d.length;
            var g = $("<tr>").appendTo($("<tfoot>").appendTo(f));
            $("<th>").attr("colspan", h + 1).text("总计：" + b.length + " 行").appendTo(g);
            for (var c = 0; c < e; c++) {
                var j = $("<td>").text(d[c] !== undefined ? LIBS.money(d[c], 1, c != 0) : "").appendTo(g);
                if (c == 6 || c == 14 || c == 12) {
                    j.addClass("result color")
                }
            }
        }).stupidtable({
            wrap: function(e, c, d) {
                if (a($(e[1]))) {
                    return 1
                } else {
                    if (a($(c[1]))) {
                        return - 1
                    }
                }
                if (d) {
                    return d(e, c)
                }
                return 0
            },
            number: function(d, c) {
                return Number(d.split(",").join("")) - Number(c.split(",").join(""))
            }
        }).find(".sortable").append("<i>").filter(".sortdefault").click()
    }
});
function showBackupkey(a) {
    var d = $("#backupPanel");
    if (d.length == 0) {
        d = $('<div id="backupPanel">').appendTo("body");
        d.dialog({
            autoOpen: false,
            title: "备份密钥",
            width: 500
        })
    }
    if (a != undefined) {
        d.empty();
        var b = $("<p>").appendTo(d);
        var c = "重置备份密钥";
        if (!a) {
            b.text("您尚未设置备份密钥");
            c = "请点此设置"
        } else {
            b.text("您的备份密钥为：").append($("<span>").text(a))
        }
        $('<input type="button">').val(c).appendTo(d).click(function() {
            $.ajax({
                url: "../backup/resetkey",
                method: "post",
                success: function(e) {
                    if (e != null) {
                        showBackupkey(e)
                    } else {
                        alert("重置失败")
                    }
                }
            })
        })
    } else {
        d.html("载入中……");
        $.ajax({
            url: "../backup/view",
            method: "post",
            success: function(e) {
                if (e != null) {
                    showBackupkey(e)
                } else {
                    d.html("载入失败，请重试。")
                }
            }
        });
        d.dialog("open")
    }
}
function selectAll(a, b) {
    $(b).parent().parent().find("input[type='checkbox']").prop("checked", a)
}
function pick(f, e, d, a, b) {
    var c = {
        username: f,
        lottery: d,
        game: a,
        date: b
    };
    if (e == 1) {
        window.open(LIBS.url("bets", c))
    } else {
        location.href = LIBS.url(location.href, c)
    }
}
function query() {
    var d = $("#search_form").attr("action");
    var c = $("input[name=lottery]:checked").map(function() {
        return $(this).val()
    }).get().join(",");
    var b = $("#filter").val();
    var a = d + "?lottery=" + c;
    if (d == "list_game") {
        a = "list_game?lottery=" + c;
        if (b) {
            a += "&username=" + b
        }
    } else {
        if (b) {
            a += "&filter=" + b
        }
    }
    a += "&begin=" + $("#begin").val() + "&end=" + $("#end").val();
    if ($("input[name=settle]:checked").val() == "true") {
        a += "&settle=true"
    }
    a += "&amount=" + $("#amount").val();
    a += "&dividend=" + $("#dividend").val();
    location.href = a
}
var dt = (function() {
    var b = 86400000;
    function a(d, c) {
        var e = "yyyy-MM-dd";
        $("#begin").val(d.format(e));
        $("#end").val(c.format(e))
    }
    return {
        day: function(e) {
            var c = new Date(TODAY + e * b);
            a(c, c)
        },
        week: function(g) {
            var e = new Date(TODAY);
            var d = e.getDay() - 1;
            if (d < 0) {
                d = 6
            }
            var f = new Date(e.getTime() - b * d + b * 7 * g);
            var c = new Date(f.getTime() + b * 6);
            a(f, c)
        },
        month: function(e) {
            var d = new Date(TODAY);
            d.setDate(1);
            d.setMonth(d.getMonth() + e);
            var c = new Date(d.getTime());
            c.setMonth(d.getMonth() + 1);
            c = new Date(c.getTime() - b);
            a(d, c)
        }
    }
})();